import unittest
from selenium import webdriver
from poium import Page, Element
import time


class LoginPre(Page):
    Login_button = Element(css='span[data-type="tuzhan"]')
    user_name = Element(css='[placeholder="请输入你的登录账户"]')
    Pass_word = Element(css='[placeholder="请输入密码"]')
    Confirm_button = Element(css='button.g-btn.login-btn.do-btn')

    def login(self, username, password):
        self.Login_button.click()
        self.user_name.input(username)
        self.Pass_word.input(password)
        self.Confirm_button.click()


class ExecuteCase(unittest.TestCase):

    def setUp(self) -> None:       # 这里将setUp 写成了setup导致报错：ExecuteCase object has no attribute 'driver'，注意大小写
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(2)
        self.driver.get("https://www.rabbitpre.com/")
        self.Login_frame()

    def Login_frame(self):  # 打开切换到登录窗
        Sin_button = self.driver.find_element_by_css_selector('#g-j-signin-btn')
        Sin_button.click()
        login_iframe = self.driver.find_element_by_css_selector('#sso')
        self.driver.switch_to.frame(login_iframe)

    def tearDown(self) -> None:
        self.driver.quit()

    def test_1(self):
        Login_page = LoginPre(self.driver)
        Login_page.login('xxx', 'xxxx')
        time.sleep(3)
        self.assertEqual(self.driver.find_element_by_css_selector('div.login-form span.error-tips').text, '登录用户不存在，请先注册', '登录失败')

    def test_2(self):
        Login_page = LoginPre(self.driver)
        Login_page.login('xxx', 'xxxx')
        time.sleep(3)
        self.assertEqual(self.driver.find_element_by_css_selector('div.login-form span.error-tips').text, '登录密码错误', '登录失败')

    def test_3(self):
        Login_page = LoginPre(self.driver)
        Login_page.login('xxx', 'xxx')
        time.sleep(3)
        self.assertEqual(self.driver.find_element_by_css_selector('a.navbar-addTemplate-btn').text, '新建作品', '登录成功')


